package com.yourcompany.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yourcompany.api.entity.Permission;
import com.yourcompany.api.service.PermissionService;
import com.yourcompany.api.service.RoleService;
import com.yourcompany.api.service.UserRoleService;
import com.yourcompany.api.vo.PermissionVO;
import com.yourcompany.api.mapper.PermissionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> 
        implements PermissionService {

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RoleService roleService;

    @Override
    public List<PermissionVO> getPermissionTree() {
        // 获取所有权限
        List<Permission> permissions = list(new LambdaQueryWrapper<Permission>()
                .orderByAsc(Permission::getSort));
        
        return buildPermissionTree(permissions);
    }

    @Override
    public List<PermissionVO> getUserPermissionTree(Long userId) {
        // 获取用户角色ID列表
        List<Long> roleIds = userRoleService.getUserRoleIds(userId);
        if (roleIds.isEmpty()) {
            return new ArrayList<>();
        }

        // 获取角色权限ID列表
        List<Long> permissionIds = roleService.getRolePermissionIds(roleIds);
        if (permissionIds.isEmpty()) {
            return new ArrayList<>();
        }

        // 获取权限列表
        List<Permission> permissions = listByIds(permissionIds);
        
        return buildPermissionTree(permissions);
    }

    @Override
    public List<PermissionVO> getRolePermissionTree(Long roleId) {
        // 获取角色权限ID列表
        List<Long> permissionIds = roleService.getRolePermissionIds(roleId);
        if (permissionIds.isEmpty()) {
            return new ArrayList<>();
        }

        // 获取权限列表
        List<Permission> permissions = listByIds(permissionIds);
        
        return buildPermissionTree(permissions);
    }

    @Override
    public List<String> getUserPermissionCodes(Long userId) {
        // 获取用户角色ID列表
        List<Long> roleIds = userRoleService.getUserRoleIds(userId);
        if (roleIds.isEmpty()) {
            return new ArrayList<>();
        }

        // 获取角色权限ID列表
        List<Long> permissionIds = roleService.getRolePermissionIds(roleIds);
        if (permissionIds.isEmpty()) {
            return new ArrayList<>();
        }

        // 获取权限列表
        return list(new LambdaQueryWrapper<Permission>()
                .in(Permission::getId, permissionIds)
                .eq(Permission::getStatus, true))
                .stream()
                .map(Permission::getCode)
                .collect(Collectors.toList());
    }

    @Override
    public List<PermissionVO> getUserMenuTree(Long userId) {
        // 获取用户角色ID列表
        List<Long> roleIds = userRoleService.getUserRoleIds(userId);
        if (roleIds.isEmpty()) {
            return new ArrayList<>();
        }

        // 获取角色权限ID列表
        List<Long> permissionIds = roleService.getRolePermissionIds(roleIds);
        if (permissionIds.isEmpty()) {
            return new ArrayList<>();
        }

        // 获取菜单列表
        List<Permission> permissions = list(new LambdaQueryWrapper<Permission>()
                .in(Permission::getId, permissionIds)
                .in(Permission::getType, 0, 1) // 目录和菜单
                .eq(Permission::getStatus, true)
                .eq(Permission::getIsVisible, true)
                .orderByAsc(Permission::getSort));

        return buildPermissionTree(permissions);
    }

    /**
     * 构建权限树
     */
    private List<PermissionVO> buildPermissionTree(List<Permission> permissions) {
        List<PermissionVO> permissionVOs = permissions.stream()
                .map(permission -> {
                    PermissionVO permissionVO = new PermissionVO();
                    BeanUtils.copyProperties(permission, permissionVO);
                    return permissionVO;
                }).collect(Collectors.toList());

        // 构建父子关系
        Map<Long, List<PermissionVO>> parentMap = permissionVOs.stream()
                .collect(Collectors.groupingBy(PermissionVO::getParentId));

        // 设置子权限
        permissionVOs.forEach(permissionVO -> 
            permissionVO.setChildren(parentMap.getOrDefault(permissionVO.getId(), new ArrayList<>())));

        // 返回顶级权限
        return parentMap.getOrDefault(0L, new ArrayList<>());
    }
} 